package com.wugenqiang.project.inventory.controller;

import com.wugenqiang.constant.ParamConstant;
import com.wugenqiang.project.inventory.pojo.Storage;
import com.wugenqiang.project.inventory.service.IStorageService;
import com.wugenqiang.project.system.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname StorageController
 * @Description TODO
 * @Date 2019/3/8 16:21
 * @Created by WuGenQiang//fighting!!
 */
@Controller
@RequestMapping("/lfd/admin/inventory/storage")
public class StorageController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(StorageController.class);

    @Resource
    private IStorageService storageService;

    @RequestMapping(value = "")
    public String ShowStorageGoodsList(Model model) {

        List<Storage> storages = null;
        LOGGER.info("start----ShowStorageGoodsList");
        storages = storageService.queryAllGoods();
        if(storages.size()==0){
            LOGGER.error("数据库表为空");
        }
        model.addAttribute("storages", storages);
        return "inventory/storage_list";
    }

    //跳转到新增物品页面
    @RequestMapping(value = "/toAddStorage")
    public String toAddStorage() {
        return "inventory/storage_add";
    }

    //新增物品
    @RequestMapping(value = "/addStorage", method = RequestMethod.POST)
    public String addStorage(Model model, HttpServletRequest request,Storage storage) {

        String result = storageService.addGoodsInfo(storage);
        if (result.equals(ParamConstant.INSERT_SUCCESS) == true) {
            List<Storage> storages = null;
            storages = storageService.queryAllGoods();
            model.addAttribute("storages", storages);
            return "inventory/storage_list";
        } else {
            return "inventory/storage_add";
        }
    }

    // 跳转到编辑页面
    @RequestMapping(value = "/toEditStorage")
    public String toEditStorage(HttpServletRequest request, Model model) {
        Integer id = Integer.parseInt(request.getParameter("id").trim());
        Storage storage = storageService.queryGoodsById(id);
        LOGGER.info(storage.toString());
        model.addAttribute("storage", storage);
        return "inventory/storage_edit";
    }

    // 更新操作
    @RequestMapping(value = "/editStorage", method = RequestMethod.POST)
    public String editStorage(Model model, HttpServletRequest request) {

        Integer id = Integer.parseInt(request.getParameter("id").trim());
        Storage storage = storageService.queryGoodsById(id);
        storage.setName(request.getParameter("name"));
        storage.setIdentifier(request.getParameter("identifier"));
        storage.setModel(request.getParameter("model"));
        storage.setAmount(Integer.valueOf(request.getParameter("amount")));
        storage.setUnit(request.getParameter("unit"));
        storage.setBalance(Integer.valueOf(request.getParameter("balance")));
        storage.setRemark(request.getParameter("remark"));
        String result = storageService.updateGoodsInfo(storage);
        if (result.equals(ParamConstant.UPDATE_SUCCESS) == true) {
            List<Storage> storages = null;
            storages = storageService.queryAllGoods();
            model.addAttribute("storages", storages);
            return "inventory/storage_list";
        } else {
            return "inventory/storage_edit";

        }
    }

    //通过name模糊查询
    @RequestMapping(value = "/queryStorageLikeName")
    public String queryStorageLikeName(@RequestParam("pattern") String pattern, Model model) {

        Storage storage = new Storage();
        storage.setName(pattern);
        /* 查询信息 */
        List<Storage> storages = storageService.queryGoodsByLikeGoodsName(storage);
        model.addAttribute("storages", storages);

        return "inventory/storage_list";
    }

    //通过model模糊查询
    @RequestMapping(value = "/queryStorageLikeModel")
    public String queryStorageLikeModel(@RequestParam("pattern") String pattern, Model model) {

        Storage storage = new Storage();
        storage.setModel(pattern);
        /* 查询信息 */
        List<Storage> storages = storageService.queryGoodsByLikeGoodsModel(storage);
        model.addAttribute("storages", storages);

        return "inventory/storage_list";
    }
}
